/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * 
 * Copyright 2010 yura, Version 2.0 (the "License");
 *
 * you may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 
 * Unless required by applicable law or agreed to in writing, software
 *
 * distributed under the License is distributed on an "AS IS" BASIS,
 *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */

package org.opu.doc2db.document.datadocument;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.simpleframework.xml.Element;

/**
 *  Table row, which contains cells list and row height
 * 
 * @author yura
 */
public class TableRow extends ParagraphList<TableCell> {

    /** height of row */
    @Element(required=false)
    private Metric<Integer> height;

    /**
     *  Create row with all argument
     * @param id id of paragraph
     * @param cells cells collections
     * @param height height of row
     */
    public TableRow(String id,
            Collection<TableCell> cells,
            Metric<Integer> height) {
        super(id, cells);
        this.height = height;
        super.dataSeparator = "\t";
    }

    /**
     *  Create row with 0 height<br>
     *<pre>
     *     this(id, cells, 0);
     *</pre>
     * @param id id of paragraph
     * @param cells cells collections
     */
    public TableRow(String id,
            Collection<TableCell> cells) {
        this(id, cells, Metric.emptyMetric());
    }

    /**
     *  Create row with 0 paragraph id<br>
     *<pre>
     *   this(0, cells, height);
     *</pre>
     * @param cells
     * @param height
     */
    public TableRow(
            Collection<TableCell> cells,
            Metric<Integer> height) {
        this("", cells, height);
    }

    /**
     *  Create row with 0 paragraph id and height id<br>
     *<pre>
     *    this(cells, 0);
     *</pre>
     * @param cells
     */
    public TableRow(
            Collection<TableCell> cells) {
        this(cells, Metric.emptyMetric());
    }

    /**
     *  create empte row without arguments
     *<pre>
     *    this(null);
     *</pre>
     */
    public TableRow() {
        this(null);
    }


    /**
     * Get the value of height
     *
     * @return the value of height
     */
    public Metric<Integer> getHeight() {
        return height;
    }

    /**
     * Set the value of height
     *
     * @param height new value of height
     */
    public void setHeight(Metric<Integer> height) {
        this.height = height;
    }

    /**
     *  Returns an array containing all of the elements
     * if cells list
     *
     * @return array containing all of the cells
     */
    public TableCell[] cellsToArray() {
        Object[] obj = elements.toArray();
        TableCell[] ret = new TableCell[obj.length];

        System.arraycopy(obj, 0, ret, 0, obj.length);

        return ret;
    }

    /**
     *  Appends the TableCell to the end of cells list.
     * And if cell was appended returned pointer on it
     *
     * @deprecated use: cellsAdd(TableCell e);
     * @return appeded TableCell, or null some error
     */
    public TableCell cellsAddNew(){
        TableCell ret = null;

        TextParagraph textParagraph = new TextParagraph();
        List<TextParagraph> asList = Arrays.asList(textParagraph);
        TableCell tableCell = new TableCell(asList);

        if (elementsAdd(tableCell)){
            int sz = elements.size() - 1;
            ret = elements.get(sz);
        }

        return ret;
    }

}
